<?php
namespace app\shop\controller;
use think\Controller;
use think\Request;
use think\Db;
use app\service\roominfoService;
use app\service\shopService;
use app\service\lifeapiService;
class Room extends Common{

	public function index(){
        //session("user_id","2088112618452297");
		session("url",request()->url());
		$userinfo=Db::table('zz_user')->where('user_id',session('user_id'))->find();
        if(empty($userinfo)){
            return "请取消重新关注";
        }
		//未绑定房屋
		if($userinfo["out_room_id"]=="0"){
			header("Location:https://www.jfzhiwen.com/shop/room/bind");
			return ;
		}
		$roominfo=Db::table("roominfo")->where("out_room_id",$userinfo["out_room_id"])->find();
		
		$roominfo["community_name"]=$userinfo["community_name"];
        //var_dump($roominfo);die;
		$this->assign("room",$roominfo);
		return $this->fetch();
	}

	//房屋绑定页面
	public function bind(){
		session("url",request()->url());
        if(request()->isPost()){
            $room_id=request()->param("roomid");
            $commnuity_id=request()->param("community_id");
            $roominfo=(new roominfoService())->roominfoIdList($room_id);
            $commnuity=(new roominfoService())->communityIdName($commnuity_id);
            $userinfo["out_room_id"]=$roominfo["out_room_id"];
            $userinfo["room_address"]=$roominfo["address"];
            $userinfo["community_id"]=$commnuity_id;
            $userinfo["community_name"]=$commnuity["community_name"];
            $res=(new shopService())->userEdit(session('user_id'),$userinfo);
            if($res){
            	//推送绑定成功信息
                $msg["title"]="您刚才进行了绑定房屋操作";
                $msg["user_id"]=session('user_id');
                $msg["url"]="https://www.jfzhiwen.com/shop/room/index";
                $msg["time"]=date('Y-m-d H:i:s');
                $msg["con"]="房屋地址：".$userinfo["community_name"].$userinfo["room_address"];
                $msg["remark"]="";
                $api = new lifeapiService();
                $result = $api->bindRoomMsg(json_encode($msg));
            	//return $this->fetch('bindsuccess');
                $url='http://p.alipay.com/P/2r6I372V';
                echo "<script>alert('房屋绑定成功');location.href='$url';</script>";
                exit;
            }else{

            }
        }else{
            $community=Db::table("community")->order('sort')->select();
            $this->assign("community",$community);

            return $this->fetch();   
        }
	}
    public function bindsuccess(){
        return $this->fetch();
    }
	public function getgroup(){
        $communityid=request()->param("communityid");
        $group=Db::table('roominfo')->distinct(true)->field('zz_group')->where("community_id",$communityid)->select();
        $str="<option value=''>请选择组团</option>";
        foreach ($group as $key => $value) {
           $str=$str."<option value='".$value["zz_group"]."'>".$value["zz_group"]."</option>";
        }
        echo $str;
    }

    public function getbuilding(){
        $communityid=request()->param("communityid");
        $group=request()->param("group");
        $building=Db::table('roominfo')->distinct(true)->field('building')->where("community_id",$communityid)->where("zz_group",$group)->select();
        /*if(empty($group)){
            $building=Db::table('roominfo')->distinct(true)->field('building')->where("community_id",$communityid)->select();
        }*/
        $str="<option value=''>请选择幢号</option>";
        foreach ($building as $key => $value) {
           $str=$str."<option value='".$value["building"]."'>".$value["building"]."</option>";
        }
        echo $str;
    }
    public function getunit(){
        $communityid=request()->param("communityid");
        $group=request()->param("group");
        $building=request()->param("building");
        $unit=Db::table('roominfo')->distinct(true)->field('unit')->where("community_id",$communityid)->where("zz_group",$group)->where("building",$building)->select();
        /*if(empty($group)){
           $unit=Db::table('roominfo')->distinct(true)->field('unit')->where("community_id",$communityid)->where("building",$building)->select();
        }*/
        $str="<option value=''>请选择单元</option>";
        foreach ($unit as $key => $value) {
           $str=$str."<option value='".$value["unit"]."'>".$value["unit"]."</option>";
        }
        echo $str;
    }
    public function getroomid(){
        $communityid=request()->param("communityid");
        $group=request()->param("group");
        $building=request()->param("building");
        $unit=request()->param("unit");
        $roomid=Db::table('roominfo')->field('id,room')->where("community_id",$communityid)->where("zz_group",$group)->where("building",$building)->where("unit",$unit)->select();
        /*if(empty($group)){
           $roomid=Db::table('roominfo')->field('id,room')->where("community_id",$communityid)->where("building",$building)->where("unit",$unit)->select();
        }*/
        $str="<option value=''>请选择房间号</option>";
        foreach ($roomid as $key => $value) {
           $str=$str."<option value='".$value["id"]."'>".$value["room"]."</option>";
        }
        echo $str;
    }
}